/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 4, 2013  11:58:55 AM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.database.dao.promis.impl
 * File Name : CustomerDaoImpl.java
 * TODO
 */
package com.tis.database.dao.promis.impl;

import java.util.Collections;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import promis.utility.CommonService;
import com.tis.database.dao.impl.AbstractDaoImpl;
import com.tis.database.dao.promis.CustomerDao;
import com.tis.database.entitys.promis.Customer;
import com.tis.database.entitys.staging.SdCustomerOutb1;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 4, 2013 : 11:58:55 AM 
 * CreateBy=Chatree Siangklom
 * TODO
 */
@Repository
@SuppressWarnings("unchecked")
public class CustomerDaoImpl extends AbstractDaoImpl<Customer, String> implements CustomerDao {
	
	private static final Logger logging = LoggerFactory.getLogger(CustomerDaoImpl.class);

	/**
	 * Date =Oct 4, 2013 : 11:59:46 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param entityClass
	 */
	protected CustomerDaoImpl() {
		super(Customer.class);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#save(java.lang.Object)
	 * Date =Oct 4, 2013 : 12:08:12 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void save(Customer e) throws HibernateException {
		super.save(e);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#save(java.util.List)
	 * Date =Oct 4, 2013 : 12:08:23 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void save(List<Customer> e) throws HibernateException {
		super.save(e);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#saveOrUpdate(java.lang.Object)
	 * Date =Oct 4, 2013 : 12:08:18 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void saveOrUpdate(Customer e) throws HibernateException {
		super.saveOrUpdate(e);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#saveOrUpdate(java.util.List)
	 * Date =Oct 4, 2013 : 12:08:49 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void saveOrUpdate(List<Customer> e) throws HibernateException {
		super.saveOrUpdate(e);
	}
	
	/**
	 * (Javadoc)
	 * @see com.tis.database.dao.promis.CustomerDao#FindByIs(java.lang.String, java.lang.String, java.lang.String, int)
	 * Date =Oct 30, 2013 : 1:58:34 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<Customer> FindByIs(String status, String syncIsstaging, String syncIssync, int maxResult) throws Exception {
		try{
			Session session = getCurrentSession();		
			Query q = session.getNamedQuery(Customer.findByIs);
			
			String query = q.getQueryString();
			
			if(!CommonService.IsNull(status))
				query += " And upper(c.status) IN("+ status +") ";
			
			q = session.createQuery(query);	
			q.setString("syncIsstaging", syncIsstaging);
			q.setString("syncIssync", syncIssync);
			
			logging.info("status=" + status + "//syncIsstaging=" + syncIsstaging + "//syncIssync=" + syncIssync);
			
			if(maxResult > 0)
				q.setMaxResults(maxResult);
			
			List lst = q.list();
			logging.info("Size=" + CommonService.getSize(lst));
			lst = Collections.checkedList(q.list(), Customer.class);
			
			if(CommonService.getSize(lst) > 0 )
				return lst;
			else 
				return null;
			
		}catch(Exception e){
			e.printStackTrace();	
		}
		return null;
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.promis.CustomerDao#Search(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, int)
	 * Date =Oct 4, 2013 : 11:59:29 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<Customer> Search(	String customerCode, 
									String idNo,
									String taxId, 
									String createdOnStart, 
									String createdOnEnd,
									int maxResult) throws HibernateException, Exception {

		try {
			
			Session session = getCurrentSession();
			Query q = session.getNamedQuery(Customer.findBySearch);
			String query = q.getQueryString();
			
			if(!CommonService.IsNull(customerCode))
				query += " And upper(c.id) like '%"+ CommonService.IsNullRString(customerCode).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(idNo))
				query += " And upper(c.idNo) like '%"+ CommonService.IsNullRString(idNo).toUpperCase() +"%' ";
			
			if(!CommonService.IsNull(taxId))
				query += " And upper(c.taxId) like '%"+ CommonService.IsNullRString(taxId).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(createdOnStart) && !CommonService.IsNull(createdOnEnd))
				query += " And to_char(c.createdOn, 'YYYY-MM-DD') BETWEEN :createdOnStart And :createdOnEnd ";
			
			query += " Order by :p_orderby "; 	
			
			logging.info(query);
			
			q = session.createQuery(query);
			
			if(!CommonService.IsNull(createdOnStart))
				q.setParameter("createdOnStart", CommonService.IsNullRString(createdOnStart).toUpperCase());
			
			if(!CommonService.IsNull(createdOnEnd))
				q.setParameter("createdOnEnd", CommonService.IsNullRString(createdOnEnd).toUpperCase());
			
			q.setParameter("p_orderby", "ACS");
			
			if(maxResult > 0)
				q.setMaxResults(maxResult);			
			
			List<Customer> lst = Collections.checkedList(q.list(), Customer.class);
			if(CommonService.getSize(lst) > 0 )
				return lst;
			else 
				return null;
			
		}catch(HibernateException ex) {
			logging.error(ex.getMessage());
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex1) {
			logging.error(ex1.getMessage());
			ex1.printStackTrace();
			throw ex1;
		}
	}
}
